/*
 * @Author: your name
 * @Date: 2020-12-24 22:22:44
 * @LastEditTime: 2020-12-26 10:49:40
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \workSpace\bilibili\src\router\index.js
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import { Toast } from 'vant';
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import("@/views/Home.vue"),
    meta: {
      keepalive: true
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import("@/views/Register.vue")
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import("@/views/Login.vue")
  },
  {
    path: '/userinfo',
    name: 'UserInfo',
    component: () => import("@/views/UserInfo.vue"),
    meta: {
      isToken: true
    }
  },
  {
    path: '/edit',
    name: 'Edit',
    component: () => import("@/views/Edit.vue"),
    meta: {
      isToken: true
    }
  },
  {
    path: '/article/:id',
    name: 'Article',
    component: () => import("@/views/Article.vue")
  },
  {
    path: '/categorychoose',
    name: 'CategoryChoose',
    component: () => import("@/views/CategoryChoose.vue")
  },
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if ((!JSON.parse(localStorage.getItem('id')) || !JSON.parse(localStorage.getItem('token'))) && to.meta.isToken == true) {
    console.log("兄弟你没登录");
    router.push('/login')
    Toast.fail('请先登录')
  } else {
    next()
  }

})
export default router
